<template>
	<div class="real-model">
		<div class="model-content">
			<!-- 头部主题 -->
			<div class="top-title">实时告警</div>
			<!-- 筛选展示部分 -->
			<div class="filter-show">
				<!-- 筛选部分 -->
				<div class="filter">
					<span class="filter-text">告警状态</span>
					<el-select v-model="value" @change="selectSystem">
						<el-option v-for="item in systemAll" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</div>
				<!-- 数据显示部分 -->
				<div class="show">
					<div class="show-total">
						<span>总数: </span>
						<span class="total-text">{{pageationObj.total}}</span>
					</div>
				</div>
			</div>
			<uni-table border stripe emptyText="暂无更多数据" style="padding: 0 20px;margin-top: 20px;max-height: 650px;">
				<!-- 表头行 -->
				<uni-tr>
					<uni-th align="left">设备类型</uni-th>
					<uni-th align="left">所属系统</uni-th>
					<uni-th align="left">设备状态</uni-th>
					<uni-th align="left">设备ID</uni-th>
					<uni-th align="left">告警信息</uni-th>
					<uni-th align="left">具体时间</uni-th>
					<uni-th align="left">位置描述</uni-th>
				</uni-tr>
				<!-- 表格数据行 -->
				<uni-tr v-for="(item,index) in tableData" :key="index+item.code" @click.native="toAlarmFault(item.messageId)">
					<uni-td>{{item.deviceTypeName }}</uni-td>
					<uni-td>{{item.systemName}}</uni-td>
					<uni-td :style="{'color':item.color}" >{{item.statusName}}</uni-td>
					<uni-td>{{item.code}}</uni-td>
					<uni-td>{{item.alarmReason}}</uni-td>
					<uni-td>{{item.createTime}}</uni-td>
					<uni-td>{{item.addressDesc}}</uni-td>
				</uni-tr>

			</uni-table>
			<div class="pagination">
				<page-pagination :total="pageationObj.total" :pageSize="10"
					:currentPage="pageationObj.pageNum" @change="changePageation"></page-pagination>
			</div>

			<!-- 关闭按钮 -->
			<image src="../../static/img/close.png" mode="" class="close-png pointer" @tap="closeModel"></image>


		</div>
		<!-- 报警弹窗 -->
		<alarm-device ref='alarmDialog' v-if="alarmDialog" :isSpeaker.sync="isSpeaker" @closeModel="isShowModel">
		</alarm-device>
		<!-- 故障弹窗 -->
		<fault-model ref='faultDialog' v-if="faultDialog" @closeFaultModel="isShowFaultModel"></fault-model>
	</div>
</template>

<script>
	import alarmDevice from "@/component/model/alarm-model.vue"
	import faultModel from "@/component/model/falut-model.vue"
	import deviceApi from "@/api/device.js"
	export default {
		props: {
			deptId: {
				type: Number,
				default: 0
			},
			buildId:{
				type:Number,
				default:0
			}
		},
		components: {
			alarmDevice,
			faultModel
		},
		data() {
			return {
				systemAll: [{
						value: '',
						label: '全部状态'
					},
					{
						value: 0,
						label: '故障'
					},
					{
						value: 1,
						label: '报警'
					},
					{
						value: 4,
						label: '离线'
					}
				],
				tableData: [],
				currentStatus: "",
				value: '',
				endDate:'',
				startDate:'',
				pageationObj:{
					pageSize:10,
					pageNum:1,
					total:0
				},
				alarmDialog:false,
				faultDialog:false,
				isSpeaker:false
			}
		},
		created() {
			//时间初始化,往前推七天
			let oneDay = 24 * 60 * 60 * 1000
			let endTime = new Date(Date.now() - oneDay)
			this.endDate = this.formatterDate(endTime, "yyyy-MM-dd")
			let startTime = new Date(Date.now() - 7 * oneDay)
			this.startDate = this.formatterDate(startTime, "yyyy-MM-dd")
			this.requestFaultDevice()
		},
		mounted() {
			let that = this; // 箭头函数的写法可省略这行代码
			let trDoms = document.getElementsByClassName('uni-table-tr');
			let len = trDoms.length;
			for(let i = 0; i < len; i++) {
				let item = trDoms[i];
				 // table为表格绑定的数据
				 // 表头行除外直接通过this.tableData[i - 1] 是否存在判断即可
				if(this.tableData[i - 1]) {

					// 这里使用了箭头函数的写法，如果使用的function 的写法，请注意this的指向问题
					item.onclick = () => {
						let row = this.tableData[i - 1];
						// this.getCurrentRow(row);
					};
					// function 的写法：
					// item.onclick = function() {
					//     let row = that.tableData[i - 1];
					//     that.getCurrentRow(row);
					// };
				}
			}
		},
		methods: {
			//时间格式化
			formatterDate(date, fmt) {
				let nowDate = {
					yyyy: date.getFullYear(), // 年
					MM: date.getMonth() + 1, // 月份
					dd: date.getDate(), //日
					hh: date.getHours(),
					mm: date.getMinutes(),
					ss: date.getSeconds()
				}
				if (/(y+)/.test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
				}
				for (var k in nowDate) {
					if (new RegExp("(" + k + ")").test(fmt)) {
						fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (nowDate[k]) : (("00" + nowDate[k]).substr((
							"" + nowDate[k]).length)));
					}
				}
				return fmt;
			},
			closeModel() {
				//关闭故障弹窗
				this.$emit('closeRealModel', false)

			},
			async requestFaultDevice() {
			// let res	=await deviceApi.getListDevice({
			// 		pageNum: this.pageationObj.pageNum,
			// 		pageSize:10,
			// 		buildId: this.buildId,
			// 		floor:'-10000',
			// 		deptId: this.deptId,
			// 		isDept: 0,
			// 		system:'',
			// 		status: this.currentStatus
			// 	})
			let res	=await deviceApi.getDeviceAlarm({
					pageNum: this.pageationObj.pageNum,
					pageSize:10,
					buildId: this.buildId,
					// floor:'-10000',
					deptId: this.deptId,
					isDept: 0,
					system:'',
					status: this.currentStatus,
					confirm:0 //未处理
				})
				this.pageationObj.total=res.data.total
				this.tableData = res.data.list.filter(v => {
					if (v.status === 1) {
						v.statusName = '报警'
						v.color = "rgba(255, 61, 61, 1)"
						return v
					} else if (v.status === 4) {
						v.statusName = '离线'
						v.color = "rgba(203, 206, 220, 1)"
						return v
					} else if (v.status === 0) {
						v.statusName = '故障'
						v.color = "rgba(254, 169, 64, 1)"
						return v
					}
				})

			},
			// 选择系统
			selectSystem(e) {
				this.pageationObj.pageNum=1
				this.currentStatus = e
				this.requestFaultDevice()

			},
			changePageation(e){
				this.pageationObj.pageNum=e
				this.requestFaultDevice()
			},
			toAlarmFault(id){
				this.tableData.forEach(v=>{
					if (v.messageId === id && v.status === 1) {
						console.log(v.status)
						this.alarmDialog = true
					} else if (v.messageId === id && v.status === 0) {
						this.faultDialog = true
					}
				})
			},
			//关闭报警弹窗
			isShowModel(val) {
				this.alarmDialog = val.show
			},
			//关闭故障弹窗
			isShowFaultModel(val) {
				this.faultDialog = val.show
			}
		}
	}
</script>

<style lang="scss" scoped>
	.real-model {
		width: 100vw;
		height: 100vh;
		background: rgba(0, 0, 0, 0.7);
		position: fixed;
		left: 0;
		top: 0;
		z-index: 998;
		overflow-y: auto;

		.model-content {
			position: absolute;
			left: 50%;
			top: 5%;
			transform: translate(-50%, 0);
			// width: 1560px;
			width: 85%;
			height: 900px;
			background: #01192E;
			z-index: 3;

			//头部主题
			.top-title {
				margin-top: 28px;
				text-align: center;
				color: rgba(2, 195, 255, 1);
				font-size: 18px;
			}
			.top-title::after {
				content: '';
				display: block;
				position: absolute;
				width: 100%;
				top: 60px;
				opacity: 0.1;
				border: 1px solid #1ACFFF;
			}

			//筛选展示部分
			.filter-show {
				margin: 56px 0 0 20px;
				display: flex;

				.filter {
					.filter-text {
						color: #fff;
						margin-right: 10px;
					}

					.select {
						width: 220px;
						display: inline-block;
						flex: 0;
						height: 44px;

						.uni-select {
							border: 1px solid rgba(2, 195, 255, 1);
						}

					}

				}

				.show {
					margin-left: 30px;
					display: flex;
					color: #fff;
					align-items: center;
					font-size: 14px;

					div {
						margin-right: 40px;

						span {
							margin-right: 10px;
						}
					}

					.total-text {
						color: rgba(2, 195, 255, 1);
					}

					.fault-text {
						color: rgba(250, 147, 6, 1);
					}

					.offline-text {
						color: rgba(203, 206, 220, 1);
					}
				}

			}

			//关闭按钮
			.close-png {
				position: absolute;
				width: 17px;
				height: 19px;
				right: 20px;
				top: 19px;
			}
		}
	}

	.uni-table-td {
		color: rgba(255, 255, 255, 0.7);
		border-bottom: 1px solid rgba(2, 195, 255, 0.25);
		padding: 13px 10px;
	}
	.uni-table{
		.uni-table-tr{
			cursor: pointer;
			&:last-child{
				.uni-table-td{
					border-bottom: none;
				}
			}

		}
	}

	.uni-table-th {
		cursor: default;
		color: rgba(255, 255, 255, 1);
	}
	::v-deep .page-pagination .page-con {
		margin-top: 20px;
		height: 60px;
		display: flex;
		justify-content: flex-end;
	}

	::v-deep .page-pagination .page-con .page-scroll-child .pag-btn {
		margin-right: 20px;
		width: 35px;
		height: 35px;
		border-radius: 35px;
		color: #fff !important;
		padding: 0;
		text-align: center;
		border: none;
		line-height: 35px;
		background-color: rgba(0, 85, 157, 0.1000);
		// margin: 0;
	}

	::v-deep .page-pagination .page-con .page-scroll-child .pag-btn:first-child {
		border: none;
	}

	::v-deep .page-pagination .page-con .page-scroll-child .pag-btn.prev {
		padding: 0;
	}

	::v-deep .page-pagination .page-con .page-scroll-child .pag-btn.next {
		padding: 0;
	}

	::v-deep .page-pagination .page-con .page-scroll-child .pag-btn.active {
		background-color: rgba(2, 195, 255, 1) !important;
		color: #fff;
	}
	.pagination{
		position: fixed;
		right: 0;
	}
	//取消表格空数据时取消右边框
	::v-deep .empty-border{
		border: none;
	}
</style>
